#include "login.h"
#include "register.h"
#include "widget.h"
#include "vedio.h"
#include "MainMenu.h"
#include <QMessageBox>
#include <QRandomGenerator>
#include <QDebug>
#include "widget.h"

Login::Login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);
    generateCaptcha();
    setWindowTitle("登录界面");
    qDebug() << "Login 窗口创建";

}

Login::~Login()
{
    delete ui;
    if (forgotPwdWidget) {
        delete forgotPwdWidget;  // 释放忘记密码窗口内存
    }
    qDebug() << "Login 窗口销毁";
}

// 生成验证码
void Login::generateCaptcha()
{
    int num1 = QRandomGenerator::global()->bounded(1, 10);
    int num2 = QRandomGenerator::global()->bounded(1, 10);
    int op = QRandomGenerator::global()->bounded(4);
    QString opStr;

    switch (op) {
        case 0: captchaResult = num1 + num2; opStr = "+"; break;
        case 1: captchaResult = num1 - num2; opStr = "-"; break;
        case 2: captchaResult = num1 * num2; opStr = "*"; break;
        case 3:
            if (num2 == 0) num2 = 1; // 避免除以 0
            captchaResult = num1 / num2;
            opStr = "/";
            break;
    }
    ui->label_captcha->setText(QString("%1 %2 %3 = ?").arg(num1).arg(opStr).arg(num2));
    qDebug() << "生成验证码：" << num1 << opStr << num2 << "=" << captchaResult;
}

// 验证验证码
bool Login::verifyCaptcha(const QString& input)
{
    bool ok;
    int userInput = input.toInt(&ok);
    qDebug() << "验证验证码：用户输入" << userInput << "，正确结果" << captchaResult;
    return ok && (userInput == captchaResult);
}

// 登录按钮点击事件
void Login::on_loginButton_clicked()
{
    QString id = ui->lineEdit_id->text().trimmed();
    QString pwd = ui->lineEdit_pass->text().trimmed();
    QString captcha = ui->lineEdit_3->text().trimmed();

    if (id.isEmpty() || pwd.isEmpty() || captcha.isEmpty()) {
        QMessageBox::warning(this, "提示", "请填完整信息！");
        return;
    }

    if (!verifyCaptcha(captcha)) {
        QMessageBox::warning(this, "提示", "验证码错误！");
        generateCaptcha();
        return;
    }

    QMessageBox::information(this, "提示", "登录成功！");
    qDebug() << "登录成功，ID：" << id;

    // 创建并显示主菜单窗口
    MainMenu *mainMenu = new MainMenu();
    mainMenu->setAttribute(Qt::WA_DeleteOnClose);
    mainMenu->show();

    // 关闭登录窗口
    this->close();
    qDebug() << "登录窗口关闭，主菜单显示";

//    Vedio *mainWindow = new Vedio();
//    mainWindow->setAttribute(Qt::WA_DeleteOnClose);
//    mainWindow->show();

//    // 关闭登录窗口
//    this->close();
//    qDebug() << "登录窗口关闭，主窗口显示";
}

// 注册按钮点击事件
void Login::on_registerButton_clicked()
{
    if (registerWidget) {
        delete registerWidget;
        registerWidget = nullptr;
    }
    registerWidget = new Register(nullptr); // parent设为nullptr
    registerWidget->setAttribute(Qt::WA_DeleteOnClose);
    connect(registerWidget, &Register::registerSuccess, this, [this]() {
        this->show();
        if (registerWidget) {
            registerWidget->deleteLater();
            registerWidget = nullptr;
        }
    });
    this->hide();
    registerWidget->show();
}

// 忘记密码按钮点击事件
void Login::on_forgotPwdButton_clicked()
{
    if (forgotPwdWidget) {
        delete forgotPwdWidget;
        forgotPwdWidget = nullptr;
    }
    forgotPwdWidget = new ForgotPwd(nullptr); // parent设为nullptr
    forgotPwdWidget->setAttribute(Qt::WA_DeleteOnClose);
    connect(forgotPwdWidget, &ForgotPwd::closed, this, [this]() {
        this->show();
        forgotPwdWidget = nullptr;
    });
    this->hide();
    forgotPwdWidget->show();
}
// 忘记密码窗口关闭后触发的槽函数
void Login::onForgotPwdClosed()
{
    qDebug() << "忘记密码窗口关闭，显示登录窗口";
    this->show();

    // 释放忘记密码窗口内存
    if (forgotPwdWidget) {
        delete forgotPwdWidget;
        forgotPwdWidget = nullptr;
    }
}

// 新增：刷新验证码按钮点击事件
void Login::on_btn_refreshCaptcha_clicked()
{
    generateCaptcha();
    qDebug() << "点击刷新验证码按钮，重新生成验证码";
}